//
//  IGMyCategoryView.m
//  jingdongfang
//
//  Created by 郭彩霞 on 2018/5/14.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGMyCategoryView.h"
#import "KLCPopup.h"
#import "iGGetCategoryApi.h"
#import "MyLikeCategoryCell.h"
#import "iGSetCategoryApi.h"
#import "AppDelegate.h"

@interface IGMyCategoryView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UIView *titleView;
    UICollectionView *collectionView;
    UIButton *nextBtn;
    NSMutableArray *selArr;
    NSInteger selCount;
    GCategoryListModel *info;
}
@property(nonatomic,strong)iGGetCategoryApi *getCategoryApi;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, copy) NSString *queryTime;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end
@implementation IGMyCategoryView
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}
+ (IGMyCategoryView *)showIGMyCategoryView {
    IGMyCategoryView *categoryView = [[IGMyCategoryView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    categoryView.backgroundColor = [UIColor whiteColor];
    [categoryView show];
    categoryView.getCategoryApi=[iGGetCategoryApi new];
    [categoryView firstPage];
    
    return categoryView;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return  self;
}
-(void)initView{
    selArr = [NSMutableArray array];
    [self settitleView];
    [self setcollectview];
    [self setnextStep];
    
}
- (void)firstPage
{
    _pageSize = 12;
    _pageNumber = 0;
    _queryTime = @"";
    [self requestDate];
}
-(void)requestDate{
    self.getCategoryApi.pageNumber=_pageNumber;
    self.getCategoryApi.pageSize=_pageSize;
    self.getCategoryApi.queryTime=_queryTime;
    @KWeakObj(self)
    [self.getCategoryApi startWithDecodeClass:[iGGetCategoryResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            [collectionView ig_endFooterRefreshing];
            iGGetCategoryResp *resp=responseObject;
            iGGetCategoryModel *model=resp.data;
            if (model.lstLabelSettings&&model.lstLabelSettings.count>0) {
                [self setupFooter];
            } else
            {
                [collectionView ig_endFooterRefreshingWithNoMoreData];
            }
            selfWeak.queryTime = model.time;
            if (_pageNumber==0)
            {
                _dataArr = [NSMutableArray arrayWithArray:model.lstLabelSettings];
            }
            else
            {
                [_dataArr addObjectsFromArray:model.lstLabelSettings];
                
            }
            for (int i=0; i<model.lstLabelSettings.count; i++) {
                [selArr addObject:@(NO)];
            }
            [collectionView reloadData];
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [collectionView ig_setFooterWithRefreshingBlock:^{
//        selfWeak.getCategoryApi.queryTime =_queryTime;
        _pageNumber=_pageNumber+1;
        [selfWeak requestDate];
    }];
}
- (void)show {
    KLCPopup *pop = [KLCPopup popupWithContentView:self showType:KLCPopupShowTypeSlideInFromTop dismissType:KLCPopupDismissTypeSlideOutToBottom maskType:KLCPopupMaskTypeDimmed dismissOnBackgroundTouch:YES dismissOnContentTouch:NO];
    [pop show];
}

-(void)settitleView{
    titleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 112)];
    titleView.backgroundColor=[UIColor clearColor];
    UILabel *foldLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, KSCREENWIDTH, 18)];
    foldLabel.text=@"选择你更喜欢的,让我更懂你";
    foldLabel.backgroundColor=[UIColor clearColor];
    foldLabel.font=[UIFont boldSystemFontOfSize:20];
    foldLabel.textColor=[UIColor c4Color];
    foldLabel.textAlignment=NSTextAlignmentCenter;
    foldLabel.centerX=KSCREENWIDTH/2;
    [titleView addSubview:foldLabel];
    
    UILabel *reLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, foldLabel.bottom+15, KSCREENWIDTH, 14)];
    reLabel.text=@"我们将努力推荐您感兴趣的内容";
    reLabel.font=[UIFont font14];
    reLabel.backgroundColor=[UIColor clearColor];
    reLabel.textColor=[UIColor c5Color];
    reLabel.textAlignment=NSTextAlignmentCenter;
    reLabel.centerX=KSCREENWIDTH/2;
    [titleView addSubview:reLabel];
    [self addSubview:titleView];
}
-(void)setcollectview{
    collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    collectionView.backgroundColor=[UIColor c3Color];
    collectionView.delegate = self;
    collectionView.showsVerticalScrollIndicator=NO;
    collectionView.dataSource = self;
    [collectionView registerNib:[UINib nibWithNibName:@"MyLikeCategoryCell" bundle:nil] forCellWithReuseIdentifier:@"MyLikeCategoryCell"];
    [self addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(44);
        make.right.mas_offset(-44);
        make.bottom.mas_offset(-39-44);
        make.top.mas_offset(112 + 15);
    }];
}
-(void)setnextStep{
    nextBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.layer.cornerRadius=20;
    nextBtn.layer.masksToBounds=YES;
    nextBtn.backgroundColor=[UIColor c1Color];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
    nextBtn.titleLabel.font=[UIFont font18];
    [nextBtn addTarget:self action:@selector(nextStepClick:) forControlEvents:UIControlEventTouchUpInside];;
    [self addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(75.0/2);
        make.right.mas_equalTo(-75.0/2);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-39);
    }];
}
#pragma mark---UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.dataArr.count ? :  0;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyLikeCategoryCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"MyLikeCategoryCell" forIndexPath:indexPath];
    cell.data=_dataArr[indexPath.item];
    cell.isSelected = [selArr[indexPath.item]boolValue] ;
    [cell setNeedsLayout];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *selIndexArr = [NSMutableArray array];
    for (int i=0; i<selArr.count; i++) {
        selCount=selIndexArr.count ;
        [collectionView reloadData];
        NSNumber *s = selArr[i];
        if ([s boolValue]) {
            [selIndexArr addObject:@(i)];
        }
    }
    BOOL sel = [selArr[indexPath.item] boolValue];
    [selArr removeObjectAtIndex:indexPath.item];
    [selArr insertObject:@(!sel) atIndex:indexPath.item];
    dispatch_async(dispatch_get_main_queue(), ^{
        [collectionView reloadData];
    });
   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KSCREENWIDTH - 88 - 32) / 3, (KSCREENWIDTH - 88 - 32) / 3 + 30);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 0, 0 ,0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 16;
}

-(void)nextStepClick:(UIButton *)btn{
    @KWeakObj(self)
    NSMutableArray *selIndexArr = [NSMutableArray array];
    for (int i=0; i<selArr.count; i++) {
        NSNumber *s = selArr[i];
        if ([s boolValue]) {
            [selIndexArr addObject:@(i)];
        }
    }
    NSMutableArray *infoArr = [NSMutableArray array];
    for (NSNumber *index in selIndexArr) {
        info = _dataArr[[index integerValue]];
        [infoArr addObject:info.cid];
    }
    for (int i=0; i<selArr.count; i++) {
        NSNumber *s = selArr[i];
        if ([s boolValue]) {
            [selIndexArr addObject:@(i)];
        }
    }

    NSString * c_idStr = [infoArr componentsJoinedByString:@","];
    iGSetCategoryApi *setApi=[[iGSetCategoryApi alloc]initWithids:c_idStr];
    [setApi startWithDecodeClass:[iGSetCategoryResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            [selfWeak presentMessageTips:NSLocalizedString(@"Sent Successfully", nil)];
            [KLCPopup dismissAllPopups];
            dispatch_async(dispatch_get_main_queue(), ^{
                [kAPPDELEGATE configTabBarVC];
            });
        }
    }];  
    
}
@end
